package com.bao.grpc.server;


import com.bao.grpc.lib.UsersGrpc;
import com.bao.grpc.lib.UsersOuterClass;
import com.bao.grpc.users.mapper.UsersMapper;
import com.bao.grpc.users.model.Users;
import io.grpc.stub.StreamObserver;
import net.devh.springboot.autoconfigure.grpc.server.GrpcService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@GrpcService(UsersOuterClass.class)
public class UsersService extends UsersGrpc.UsersImplBase {

    @Autowired(required = false)
    UsersMapper usersMapper;

    @Override
    public void getUsers(UsersOuterClass.UsersRequest request, StreamObserver<UsersOuterClass.userLists> responseObserver) {
        UsersOuterClass.UsersRequest param = UsersOuterClass.UsersRequest.newBuilder().setById(request.getById()).setByName(request.getByName()).setUsPhone(request.getUsPhone()).build();
        List<Users> list=usersMapper.selectList(null);
        final UsersOuterClass.userLists.Builder replyBuilder = UsersOuterClass.userLists.newBuilder();

        for (Users user:list) {
            UsersOuterClass.users.Builder dataBuilder = UsersOuterClass.users.newBuilder();
            dataBuilder.setById(user.getById().toString());
            dataBuilder.setByName(user.getByName());
            dataBuilder.setUsPhone(user.getUsPhone());
            replyBuilder.addUserlist(dataBuilder);
        }
        responseObserver.onNext(replyBuilder.build());
        responseObserver.onCompleted();
    }

}
